package com.mj.activity.user;

import org.json.JSONObject;

import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;

import com.mj.MJApplaction;
import com.mj.R;
import com.mj.base.BaseActivity;
import com.mj.http.Json;
import com.mj.http.Url;
import com.mj.util.Constant;
import com.mj.util.Info;

public class Activity_Login extends BaseActivity {
	private String mobile, pwds;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_login);
		init();
	}

	private void init() {
		// title
		aq.id(R.id.btn_left).clicked(this, "back");
		aq.id(R.id.btn_right).gone();
		aq.id(R.id.tv_title).text("登录");
		// next
		aq.id(R.id.button1).clicked(this, "login");
	}

	private boolean check() {
		mobile = aq.id(R.id.editText1).getText().toString();
		if (TextUtils.isEmpty(mobile)) {
			aq.id(R.id.editText1).getView().requestFocus();
			Toast.makeText(this, "请输入帐号", Toast.LENGTH_LONG).show();
			return false;
		}
		pwds = aq.id(R.id.editText2).getText().toString();
		if (TextUtils.isEmpty(pwds)) {
			aq.id(R.id.editText2).getView().requestFocus();
			Toast.makeText(this, "请输入密码", Toast.LENGTH_LONG).show();
			return false;
		}
		return true;
	}

	public void login() {
		if (check()) {
			baseCallBack callBack = new baseCallBack() {
				@Override
				public void onSuccess(JSONObject object) {
					super.onSuccess(object);
					MJApplaction.userInfo = Json.getUserInfo(object);
					Info.saveUserInfo(Activity_Login.this);
					Toast.makeText(Activity_Login.this, "登录成功", Toast.LENGTH_LONG).show();
					setResult(Constant.LOGIN);
					finish();
				}
			};
			String url = Url.login + "?mobile=" + mobile + "&pwds=" + pwds;
			getJson(url, callBack, false);
		}
	}

}
